package com.wyg.epuser.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyg.epcommonbase.common.ApiResult;
import com.wyg.epcommonbase.common.annotation.Login;
import com.wyg.epcommonbase.common.util.AssertUtil;
import com.wyg.epuser.entity.EntityValidResp;
import com.wyg.epuser.entity.User;
import com.wyg.epuser.entity.UserEntity;
import com.wyg.epuser.service.CompanyNumService;
import com.wyg.epuser.service.UserEntityService;
import com.wyg.epuser.utils.HttpUtil;
import com.wyg.epuser.utils.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: WangYongGan
 * @Date: 2022/3/2 0:41
 * @Description:
 */
@RestController
@RequestMapping("userLike")
@Api(tags = "用户收藏")
public class UserEntityController {
    @Autowired
    UserEntityService userEntityService;

    @Autowired
    CompanyNumService companyNumService;

    @Login
    @ApiOperation("增加收藏")
    @PostMapping
    public ApiResult addLike(String entityName) {
        final User user = RequestUtil.getUser();
        AssertUtil.isTrue(!companyNumService.exists(entityName), 104, "该企业不存在");
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        final UserEntity one = userEntityService.getOne(wrapper.lambda().eq(UserEntity::getUid, user.getId()).eq(UserEntity::getEntity, entityName));
        AssertUtil.isTrue(one != null, 119, "收藏已存在");
        wrapper.clear();
        wrapper.lambda().eq(UserEntity::getUid, user.getId());
        AssertUtil.isTrue(userEntityService.count(wrapper) > 20, 103, "收藏已达上限");
        UserEntity userEntity = new UserEntity();
        userEntity.setUid(user.getId());
        userEntity.setEntity(entityName);
        userEntityService.saveOrUpdate(userEntity);
        return ApiResult.ok();
    }

    @Login
    @ApiOperation("添加不存在的企业")
    @PostMapping("addEntity")
    public ApiResult addEntity(@RequestParam String entityName) {
        EntityValidResp entityValidResp = HttpUtil.entityIsValid(entityName);
        System.out.println(entityValidResp);
        if (companyNumService.getCountOfCompany(entityName) != null)
            return ApiResult.error(110, "该企业数据已存在");
        if (!entityValidResp.isFlag())
            return ApiResult.error(111, "企业不存在");
        UserEntity userEntity = new UserEntity();
        userEntity.setEntity(entityName);
        userEntityService.saveOrUpdate(userEntity);
        return ApiResult.ok().data(entityValidResp.getEntityInfoRespList()).message("添加成功,1小时后可查到相关信息");
    }

    @Login
    @ApiOperation("删除收藏")
    @DeleteMapping
    public ApiResult delete(String entityName) {
        final User user = RequestUtil.getUser();
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id", user.getId()).eq("entity", entityName);
        userEntityService.remove(wrapper);
        return ApiResult.ok();
    }

    @Login
    @ApiOperation("查询收藏")
    @GetMapping
    public ApiResult get() {
        final User user = RequestUtil.getUser();
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        final List<UserEntity> list = userEntityService.list(wrapper.select("entity").lambda().eq(UserEntity::getUid, user.getId()));
        List<String> res = list.stream().map(UserEntity::getEntity).collect(Collectors.toList());
        return ApiResult.ok().data(res);
    }


}
